package com.boilerCloud.controller.sys;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.sys.User;
import com.boilerCloud.service.sys.IHzlqswUserService;

@RestController
@RequestMapping(value = "/account/")
public class AccountControler {
	@Resource
	private IHzlqswUserService userService;
	
	private static final Logger log = LoggerFactory.getLogger(AccountControler.class);


	@RequestMapping(value = "accountList")
	public ModelAndView accountList() {
		return new ModelAndView("/views/account/accountInfo.html");
	}
	@RequestMapping(value = "accountInfo")
	public ModelAndView accountInfo() {
		return new ModelAndView("/views/account/accountInfo_c.html");
	}
	

	/**
	 * 查询账号列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryAccountList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryBusinessList(@ModelAttribute User user ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<User> page = new HzlqswPage<User>();
			page.setCurPage(yema);
			page.setPageSize(length);
			userService.qryList(user,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询账号列表失败");
			log.error("查询账号列表异常",e);
			return rst;
		}
	}
	
	
	/**
	 * 新增用户
	 * @param user
	 * @param request
	 * @return
	 */
	@LogAnnotation("新增用户")
	@RequestMapping(value = "addAccount", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addAppVersion(
			@ModelAttribute User user ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			boolean success = userService.save(user);
			rst.setResultCode(ReqResult.resultCode_Success);
			if(!success){
				rst.setResultCode(ReqResult.resultCode_exist);
				rst.setReturnObject("账号已存在");
			}
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增用户失败");
			log.error("新增用户异常",e);
			return rst;
		}
	}
	
	
	@RequestMapping(value = "editAccount", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editAccount(
			@ModelAttribute User user ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			userService.edit(user);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改用户失败");
			log.error("新增用户异常",e);
			return rst;
		}
	}
	
	

	@RequestMapping(value = "getUser", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getUser(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			if(id==0){
				User uu =(User)request.getSession().getAttribute("usersession");
				id= uu.getId();
			}
			
			User use = userService.getUserByid(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(use);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改用户失败");
			log.error("新增用户异常",e);
			return rst;
		}
	}
	
	/**
	 * 重置密码
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "resetPassword", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult resetPassword(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			userService.resetPassword(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("重置密码失败");
			log.error("重置密码异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除账号
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delAccount", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delAccount(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			userService.delAccount(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除账号失败");
			log.error("删除账号异常",e);
			return rst;
		}
	}
	


	
}
